// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

import API_ACCESS from './API_ACCESS'
import API_PARKING from './API_PARKING'
import API_DEFENCE from './API_DEFENCE'
import API_PATROL from './API_PATROL'
import API_FIREFIGHTING from './API_FIREFIGHTING'
import API_BROADCAST from './API_BROADCAST'
import API_IOT from './API_IOT'

import state from '@admin/views/Secure/detail/DetailBroadcast/store/state'

const statusList = state.DICT.STATUS.map(([id]) => id)

const TIP_MAP = {
  0: '设备',
  2: '门禁',
  3: '出入口',
  4: '防区',
  16: '巡更点',
  21: '广播',
  24: '消防',
}

// ============================== 导出异步调用 ============================== //

export default {
  // —————————————————————————————— 结构树 —————————————————————————————— //

  remoteTree: () => null,

  // —————————————————————————————— 下拉框 —————————————————————————————— //

  async remoteSelect({ state: { selectParam, INIT_CONFIG: { codeKey }, API } }) {
    if (CORE.isEmpty(selectParam.nextIdList)) return []

    const multi = selectParam.nextIdList.map((areaId) => {
      const deviceCategoryId = selectParam.isIOT ? 0 : selectParam.deviceCategoryId
      const api = API_MAP[deviceCategoryId] || API_MAP[0]
      const param = getParam(selectParam, areaId)
      const TIP = `查询所在${CORE.MAP.REGION}的${TIP_MAP[deviceCategoryId] || TIP_MAP[0]}`
      return api.getSelect(param, API.transTip(TIP))
    })

    const select = await Promise.all(multi.map((promise) => promise.catch(() => null))) // 部分成功，也要返回
    return select.map((arr) => transList(arr, codeKey, selectParam.isIOT))
  },
}

// ============================== 内部方法 ============================== //

export const API_MAP = {
  0: API_IOT,
  2: API_ACCESS,
  3: API_PARKING,
  4: API_DEFENCE,
  16: API_PATROL,
  21: API_BROADCAST,
  24: API_FIREFIGHTING,
}

const getParam = ({ pageSize, areaIdQueryType, deviceCategoryId }, areaId) => {
  switch (deviceCategoryId) {
    // case '2': return { pageSize, deviceCategoryId, areaId, areaIdQueryType }
    // case '3': return { pageSize, deviceCategoryId, areaId }
    // case '4': return { pageSize, deviceCategoryId, areaId, areaIdQueryType }
    // case '16': return { pageSize, deviceCategoryId, areaId }
    case '21': return { pageSize, deviceCategoryId, areaId, statusList }
    // case '24': return { pageSize, deviceCategoryId, areaId, areaIdQueryType }
    default: return { pageSize, deviceCategoryId, areaId, areaIdQueryType }
  }
}

const KEY_MAP = {
  0: 'deviceName',
  2: 'name',
  3: 'entranceName',
  4: 'defenceName',
  16: 'patrolName',
  21: 'name',
  24: 'pointName',
}

export const transList = (arr, codeKey, isIOT) => {
  if (CORE.isEmpty(arr)) return []

  const deviceCategoryId = arr[0].deviceCategoryId
  const nameKey = KEY_MAP[isIOT ? 0 : deviceCategoryId] || KEY_MAP[0]

  arr.forEach((item) => {
    item.deviceId = item.id
    item.deviceName = item[nameKey]
    item.showName = codeKey && item[codeKey] ? `${item[nameKey]} (${item[codeKey]})` : item[nameKey]
  })

  return arr.sort((a, b) => (a[nameKey] || '').localeCompare(b[nameKey] || '', 'zh-CN'))
}
